package com.lan.entity.sys;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import java.util.List;

import com.lan.entity.Entity;
import com.lan.utils.tree.TreeNode;
import com.lan.utils.tree.TreeTableNode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotBlank;

/**
* <p>
* 菜单 -> 权限
* </p>
*
* @author shadow
* @since 2022-06-04
*/

@Data
@TableName("student_menu")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class Menu extends Entity {

    /**
     * 类型 0文件夹 1菜单项 2按钮
     */
    @NotBlank(message = "菜单类型不能为空")
    private String type;

    /**
    * 菜单名
    */
    @NotBlank(message = "菜单名不能为空")
    private String name;

    /**
     * 父级菜单id
     */
    @NotBlank(message = "父级菜单不能为空")
    private String parentId;

    /**
     * 父级菜单
     */
    @NotBlank(message = "父级菜单不能为空")
    private String parentName;

    /**
     * 权限
     */
    private String permission;

    /**
     * 图标
     */
    private String icon;

    /**
    * 路径
    */
    private String href;

    /**
    * 排序号
    */
    private Integer orderNum;

    /**
     * 子菜单
     */
    @TableField(exist = false)
    private List<Menu> children;

    public TreeNode<Menu> toTree() {
        TreeNode<Menu> tree = new TreeNode<>();
        tree.setId(this.getId());
        tree.setParentId(this.parentId);
        tree.setTitle(this.name);
        return tree;
    }

    public TreeTableNode toTreeTable() {
        TreeTableNode treeTableNode = new TreeTableNode();
        treeTableNode.setPowerId(this.getId());
        treeTableNode.setPowerName(this.name);
        if (this.type.equals("1") || this.type.equals("0")) {
            treeTableNode.setPowerType(this.type);
        } else {
            treeTableNode.setPowerType("1");
        }
        treeTableNode.setPowerCode(this.permission);
        treeTableNode.setPowerUrl(this.href);
        treeTableNode.setParentId(this.parentId);
        treeTableNode.setIcon(this.icon);
        treeTableNode.setSort(this.orderNum);
        return treeTableNode;
    }
}